﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.ServiceModel;
using System.Net;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Net.Mail;
using System.ComponentModel;
using System.Threading;
using System.Dynamic;
using System.Collections;
using System.Net.Http;
using System.Threading.Tasks;
using RestSharp;
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            //ConsoleApplication.ServiceReference.WCFServiceClient client = new ServiceReference.WCFServiceClient();
            //var name= client.GetName("zhangsan");

            //var client = new RestClient("http://localhost:8732/WCFService/");
            var client = new RestClient("http://localhost:57152/Service.svc");
            // client.Authenticator = new HttpBasicAuthenticator(username, password);

            var r = new RestRequest("GetData", Method.GET);
            r.RequestFormat = DataFormat.Json;
            r.AddParameter("value", 123, ParameterType.GetOrPost);
            IRestResponse p = client.Execute(r);

            var Request = new RestRequest("All", Method.GET);
            Request.RequestFormat = DataFormat.Json;
            //Request.AddParameter("name", "value"); // adds to POST or URL querystring based on Method
            //Request.AddUrlSegment("id",""); // replaces matching token in request.Resource
            //Request.AddParameter("name", "zhangsan", ParameterType.GetOrPost);
            // execute the request
            IRestResponse Response = client.Execute(Request);
            Console.WriteLine(Response.Content);

            Request = new RestRequest("Get/{id}", Method.GET);
            Request.RequestFormat = DataFormat.Json;
            //postRequest.AddObject(new { name = "zhangsan", age = 20 });
            //postRequest.AddBody("{\"name\":\"zhangsan\",\"age\":20}");
            //postRequest.AddBody(new { name = "zhangsan", age = 20 });
            //postRequest.AddParameter("name", "zhangsan", ParameterType.RequestBody);
            //postRequest.AddParameter("age", 20, ParameterType.RequestBody);
            Request.AddUrlSegment("id", "001");
            Response = client.Execute(Request);
            Console.WriteLine(Response.Content);

            Request = new RestRequest("Put/{id}", Method.PUT);
            Request.RequestFormat = DataFormat.Json;
            Request.AddUrlSegment("id", "001");
            //Request.AddParameter("name", "zhangfei", ParameterType.GetOrPost);
            //Request.AddParameter("sex", true, ParameterType.GetOrPost);
            //Request.AddParameter("age", 18, ParameterType.GetOrPost);
            Request.AddBody(new {name = "zhangfei", sex = true, age = 18 });
            Response = client.Execute(Request);
            Console.WriteLine(Response.Content);

            Request = new RestRequest("Post", Method.POST);
            Request.RequestFormat = DataFormat.Json;
            Request.AddBody(new { id = "006", name = "guanyu", sex = true, age = 18 });
            Response = client.Execute(Request);
            Console.WriteLine(Response.Content);

            // async with deserialization
            //var asyncHandle = client.ExecuteAsync(request, response =>
            //{
            //    Console.WriteLine(response.Content);
            //});

            // abort the request on demand
            //asyncHandle.Abort();


            /*
            int[] array = new int[] {9,4,2,1,6,8,3,7,5,0 };
            Algorithm.ShellSort(array);
            Int32 total = 1000000;
            Random rd = new Random();
            Int32[] ints = new Int32[total];
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (var i = 0; i < total; i++)
            {
                ints[i] = i;
            }
            int temp = 0;
            for (var m = total; m > 0; m--)
            {
                var index = rd.Next(m);
                temp = ints[index];
                ints[index] = ints[m - 1];
                ints[m - 1] = temp;
            }
            sw.Stop();
            Console.WriteLine("运行时间为：{0}",sw.ElapsedMilliseconds);
            */
            //SendMail();
            Console.Read();
        }

        static void StartWCF()
        {
            ServiceHost serviceHost = null;
            if (serviceHost != null)
            {
                serviceHost.Close();
            }

            // Create a ServiceHost for the CalculatorService type and 
            // provide the base address.
            serviceHost = new ServiceHost(typeof(WcfService));

            // Open the ServiceHostBase to create listeners and start 
            // listening for messages.
            serviceHost.Open();
            Console.Read();
        }

        static void SendMail()
        {
            SmtpClient client = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 25,
                EnableSsl = true,
                Credentials = new NetworkCredential("jiangzm1314@gmail.com", ""),
                DeliveryMethod = SmtpDeliveryMethod.Network
            };
            client.SendCompleted += new SendCompletedEventHandler((object sender, AsyncCompletedEventArgs e) =>
            {
                var message = (MailMessage)e.UserState;
                if (e.Cancelled)
                {
                    //发送邮箱已被取消!
                    Console.WriteLine("发送邮件被取消:" + message.Subject);
                }
                else if (e.Error == null)
                {
                    //发送邮箱成功！
                    Console.WriteLine("发送邮件成功:" + message.Subject);
                }
                else
                {
                    //发送邮箱失败
                    Console.WriteLine("发送邮件失败:" + message.Subject);
                }
                client.Dispose();
            });
            MailMessage Message = new MailMessage
            {
                BodyEncoding = Encoding.UTF8,
                From = new MailAddress("jiangzm1314@gmail.com", "Michael"),
                Subject = "这是一封测试邮件",
                Body = "时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "<br />Hello World!<br /><div><a href=\"javascript:alert('Hi!');\" target=\"_blank\"><img id=\"view_img\" border=\"0\" src=\"http://service.t.sina.com.cn/widget/qmd/1568379371/4e75a0a5/1.png?rnd=1285027662636\" onload=\"alert('你的Cookie泄漏了！');\"></a></div>",
                IsBodyHtml = true,
                Priority = MailPriority.High
            };
            Message.To.Add("741805476@qq.com");
            client.SendAsync(Message, Message);
        }
    }
}